﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XeBus.BusinessLogic;
using XeBus.Controller;
using XeBus.BusinessObject;

namespace XeBus.Controller
{
    public partial class frmLoTrinh : Form
    {
        private LoTrinhBUS ltBUS = new LoTrinhBUS();
        private LoTrinh ltObj = new LoTrinh();
        private DataTable dt = new DataTable();
        public string ma_tuyen { get; set; }
        private int y;

        public frmLoTrinh()
        {
            InitializeComponent();
            Load_Lo_Trinh();            
        }

        private void btDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Load_Lo_Trinh()
        {
            dgvLoTrinh.AutoGenerateColumns = false;
            dgvLoTrinh.DataSource = ltBUS.LoTrinh();
        }

        private void Load_Lo_Trinh(string ma_tuyen)
        {
            dgvLoTrinh.AutoGenerateColumns = false;
            dgvLoTrinh.DataSource = ltBUS.LoTrinh(ma_tuyen);
        }

        private void btThemMoi_Click(object sender, EventArgs e)
        {
            frmThemSua tlt = new frmThemSua();
            tlt.ma_tuyen = ma_tuyen;
            tlt.label2.Text = "";
            tlt.ShowDialog();
            if (ma_tuyen != null)
                Load_Lo_Trinh(ma_tuyen);
            else
                Load_Lo_Trinh();
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            try
            {
                frmThemSua tml = new frmThemSua();
                tml.label1.Text = "Sửa lộ trình: mã lộ trình ";
                tml.txtTenLoTrinh.Text = dt.Rows[0]["Ten_Lo_Trinh"].ToString();
                tml.txtHuongDi.Text = dt.Rows[0]["Huong_Di"].ToString();
                tml.label2.Text = dt.Rows[0]["Ma_Lo_Trinh"].ToString();
                tml.ShowDialog();
                if (ma_tuyen != null)
                    Load_Lo_Trinh(ma_tuyen);
                else
                    Load_Lo_Trinh();
            }
            catch (Exception) { }

        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            DialogResult  dr = MessageBox.Show("Bạn có chắc chắn muốn xóa không ? ", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                try
                {
                    y = dgvLoTrinh.CurrentCell.RowIndex;
                    ltObj.Ma_Lo_Trinh = dgvLoTrinh.Rows[y].Cells[0].Value.ToString();
                    ltBUS.XOA(ltObj.Ma_Lo_Trinh);
                    MessageBox.Show("Xóa dữ liệu thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception)
                {
                    MessageBox.Show("Xóa dữ liệu thất bại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                if (ma_tuyen != null)
                    Load_Lo_Trinh(ma_tuyen);
                else
                    Load_Lo_Trinh();
            }
        }

        private void Hien_Thi(int y)
        {
            string id = dgvLoTrinh.Rows[y].Cells[0].Value.ToString();
            dt = ltBUS.LoTrinhTheoID(id);

            lblMaLoTrinh.Text = dt.Rows[0]["Ma_Lo_Trinh"].ToString();
            lblTenLoTrinh.Text = dt.Rows[0]["Ten_Lo_Trinh"].ToString();
            lblHuongDi.Text = dt.Rows[0]["Huong_Di"].ToString();
            lblMaTuyen.Text = dt.Rows[0]["Ma_Tuyen"].ToString();
        }

        private void dgvLoTrinh_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            y = e.RowIndex;
            Hien_Thi(y);
        }
    }
}
